define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/user_coupon/index' + location.search,
                    add_url: 'chase/user_coupon/add',
                    edit_url: 'chase/user_coupon/edit',
                    del_url: 'chase/user_coupon/del',
                    multi_url: 'chase/user_coupon/multi',
                    table: 'user_coupons',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('用户名称')},
                        {field: 'coupon_id', title: __('Coupon_id')},
                        {field: 'code', title: __('Code')},
                        {field: 'qrcode', title: __('Qrcode'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'coupon_name', title: __('Coupon_name')},
                        {field: 'need_amount', title: __('Need_amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: __('Discount_amount'), operate:'BETWEEN'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'way', title: __('Way'), searchList: {"1":__('Way 1'),"2":__('Way 2'),"3":__('Way 3'),"4":__('Way 4'),}, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            if (data.status == 1) {
                                 return   `<button id="coupon-check" data-id="`+data.id+`" class="btn btn-success">核销</button>`
                            }
                        }},

                    ]
                ]
            });

            $('#coupon-code').on('click', function () {
                layer.prompt(function(value, index, elem){
                    console.log(index)
                    $.post('chase/user_coupon/code', {
                        "code": value
                    }, function (res) {

                        if (res.code == 1) {
                            layer.msg('核销成功')
                            setTimeout(()=>{
                                window.location.reload()
                            },1000)
                        } else {
                            layer.msg(res.msg)
                            layer.close(index);
                        }


                    })
                });
            })

            $('#table').on('click', '#coupon-check', function () {
                // console.log(1111)
                var id = $(this).data(id);
                // console.log(id)
                layer.confirm('您确定要核销此优惠券吗？', function () {
                    $.post('chase/user_coupon/check', id, function (res) {
                        console.log(res)
                        if (res.code == 1) {
                            layer.msg('核销成功')
                            setTimeout(()=>{
                                window.location.reload()
                            },1000)
                        } else {
                            layer.msg(res.msg)
                            layer.close(index);
                        }


                    })
                })
            })
            $('#create-coupon').onclick(function () {
                console.log(234)
            })

            // 为表格绑定事件
            Table.api.bindevent(table);


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        coupon: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/user_coupon/coupon' + location.search,
                    add_url: 'chase/user_coupon/add',
                    edit_url: 'chase/user_coupon/edit',
                    del_url: 'chase/user_coupon/del',
                    multi_url: 'chase/user_coupon/multi',
                    table: 'user_coupons',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: false,
                search: false,
                showExport: false,
                exportDataType: "",
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'user.nickname', title: __('用户名称')},
                        {field: 'coupon_id', title: __('Coupon_id')},
                        {field: 'code', title: __('Code')},
                        {field: 'qrcode', title: __('Qrcode'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'coupon_name', title: __('Coupon_name')},
                        {field: 'need_amount', title: __('Need_amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: __('Discount_amount'), operate:'BETWEEN'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'way', title: __('Way'), searchList: {"1":__('Way 1'),"2":__('Way 2'),"3":__('Way 3'),"4":__('Way 4'),}, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                        //         if (data.status == 1) {
                        //             return   `<button id="coupon-check" data-id="`+data.id+`" class="btn btn-success">核销</button>`
                        //         }
                        //     }},

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


        },
        create: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});